package com.gitee.feizns.explore.data_structure.tree.bs.easy;

/**
 * 783. 二叉搜索树结点最小距离
 * @author feizns
 * @since 2020/1/6
 */
public class MinDiffInBST {

    public static void main(String[] args) {

    }

    static int min = Integer.MIN_VALUE;
    static TreeNode prev;
    public static int minDiffInBST(TreeNode root) {
        inOrder(root);
        return min;
    }

    private static void inOrder(TreeNode node) {
        if ( node != null ) {
            inOrder(node.left);
            if ( prev != null )
                min = Math.min(min, Math.abs(prev.val - node.val));
            prev = node;
            inOrder(node.right);
        }
    }

}
